package com.example.demo.web;

import com.example.demo.domain.entity.ParameterDefinitionEntity;
import com.example.demo.domain.model.Parameter;
import com.example.demo.domain.vo.front.PageWrapper;
import com.example.demo.domain.vo.front.ParameterDefinitionFormVO;
import com.example.demo.domain.vo.front.ParameterFrontVO;
import com.example.demo.domain.vo.front.ResBody;
import com.example.demo.service.ParameterFrontService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ParameterController {
    @Autowired
    ParameterFrontService parameterFrontService;

    @GetMapping("/parameter/page")
    public PageWrapper<ParameterFrontVO> pageP(@RequestParam("pageSize") Integer pageSize,
                                               @RequestParam("pageNumber") Integer pageNumber, String code) {
        return parameterFrontService.getPage(pageSize, pageNumber, code);
    }

    @PostMapping("/parameter")
    public ResBody<Long> postP(@RequestBody Parameter parameter) {
        Long id = parameterFrontService.saveSingleP(parameter);
        return ResBody.success(id);
    }

    @DeleteMapping("/parameter/{pid}")
    public ResBody<String> removeP(@PathVariable Long pid) {
        parameterFrontService.removeSingleP(pid);
        return ResBody.success();
    }

    /***********************************************************PD****************************************************/
    @GetMapping("/parameter/definition/{pid}")
    public List<ParameterDefinitionEntity> getPD(@PathVariable("pid") Long pid) {
        return parameterFrontService.getPD(pid);
    }

    @PostMapping("/parameter/definition")
    public ResBody<Long> postPD(@RequestBody ParameterDefinitionFormVO parameterDefinitionFormVO) {
        Long aLong = parameterFrontService.saveSinglePD(parameterDefinitionFormVO);
        return ResBody.success(aLong);
    }

    @DeleteMapping("/parameter/definition/{pdid}")
    public ResBody<String> postPD(@PathVariable Long pdid) {
        parameterFrontService.removeSinglePD(pdid);
        return ResBody.success();
    }

    @GetMapping("/parameter/definition/page")
    public PageWrapper<ParameterDefinitionEntity> pagePD(@RequestParam("pageSize") Integer pageSize,
                                                         @RequestParam("pageNumber") Integer pageNumber, @RequestParam("pid") Long pid) {
        return parameterFrontService.pagePD(pageSize, pageNumber, pid);
    }

    /***********************************************************PV****************************************************/

    @GetMapping("/parameter/value/page")
    public PageWrapper<Map<String, String>> pagePV(@RequestParam("pageSize") Integer pageSize,
                                                   @RequestParam("pageNumber") Integer pageNumber, @RequestParam("pid") Long pid) {
        return parameterFrontService.getPvPage(pageSize, pageNumber, pid);
    }


    @PostMapping("/parameter/value")
    public ResBody<Long> addPV(@RequestBody Map<String, String> pv) {
        Long id = parameterFrontService.saveSinglePv(pv);
        return ResBody.success(id);
    }


    @DeleteMapping("/parameter/value/{pvid}")
    public ResBody<String> removePV(@PathVariable Long pvid) {
        parameterFrontService.removeSinglePV(pvid);
        return ResBody.success();
    }
}
